package com.sg.business.projectgroup.labelprovider;

import org.bson.types.ObjectId;
import org.eclipse.jface.viewers.ColumnLabelProvider;

import com.mobnut.db.model.ModelService;
import com.sg.business.model.ProjectGroup;
import com.sg.business.model.Work;
import com.sg.business.model.WorkDefinition;

public class WorkDefinitionDescLabelProvider extends ColumnLabelProvider {

	public WorkDefinitionDescLabelProvider() {
	}

	@Override
	public String getText(Object element) {
		if (element instanceof WorkDefinition) {
			WorkDefinition workd = (WorkDefinition)element;
			String desc = workd.getDesc();
			if (desc != null && !desc.isEmpty()) {
				return desc;
			}
		} else if(element instanceof ProjectGroup) {
			ProjectGroup projectGroup = (ProjectGroup) element;
			ObjectId id = (ObjectId)projectGroup.getValue("workd_id");
			WorkDefinition workd = ModelService.createModelObject(WorkDefinition.class, id);
			String desc = workd.getDesc();
			if (desc != null && !desc.isEmpty()) {
				return desc;
			}
		} else if(element instanceof Work) {
			Work work = (Work) element;
			WorkDefinition workDefinition = work.getWorkDefinition();
			if(workDefinition != null) {
				return workDefinition.getDesc();
			}
		}
		return "?";
	}
}
